import { GetanTableProps } from "../types"
import { isFunction, isString } from "@/utils"

/**
 * 支持单选多选
 */
export function useRowSelect(props: Ref<Partial<GetanTableProps>>) {
    /**
     * 选择行的KEY
     */
    const checkedRowKeysRef = ref<(string | number)[]>([])

    const rowKeyFunction = computed(() => {
        const { rowKey } = unref(props)
        if (isFunction(rowKey)) {
            return rowKey
        } else if (isString(rowKey)) {
            return (rowData: Recordable) => rowData[rowKey as string]
        } else {
            return (rowData: Recordable) => rowData["id"]
        }
    })

    const clearChecked = () => {
        checkedRowKeysRef.value = []
    }

    return { checkedRowKeysRef, rowKeyFunction, clearChecked }
}
